const app = getApp()
const db = wx.cloud.database();
const _ = db.command;
const util = require("../../../../utils/util.js");
Page({
  data: {
    openid: '',
    userInfo: null,
    idols: [],
    idolsID: {},
    // 翻页加载
    page: 1,
    total: 1,
    size: 10,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.authentication();
    this.loadIdol(true);
  },
  onReachBottom: function () {
    this.loadIdol()
  },
  // 翻页加载 偶像信息
  loadIdol: function (isNew) {
    // 先取出集合记录总数
    db.collection('ad_idol').count({
      success: res2 => {
        // console.info(res2)
        const total1 = res2.total
        // 计算需分几次取
        // const batchTimes = Math.ceil(total / 100)
        let that = this,
          size = this.data.size,
          page = this.data.page,
          total = Math.ceil(total1 / size);
          that.setData({
            total: total
          })
        if (isNew) {
          that.setData({
            page: 1
          })
          page = 1
        }
        if (page <= total) {
          console.log("total=" + total)
          db.collection('ad_idol')
            // .where({
            //   //text为要搜索的对象名，务必对应集合里面的名字
            //   name: db.RegExp({
            //     //从搜索栏中获取的value作为规则进行匹配。
            //     regexp: '兴',
            //     //大小写不区分
            //     options: 'i',
            //   })
            // })
            // .where({
            //   // _openid: 'xxx', // 填入当前用户 openid
            // })
            // .match({
            //   baby_id: baby.info[0]._id,
            //   deled: false
            // })
            // .sort({
            //   create_time: -1
            // })
            .skip(((page - 1) * size))
            .limit(size)
            .orderBy('create_time', 'desc')
            .get()
            .then(res => {
              console.log("page=" + page)
              console.log(res)
              // that.setData({
              //   idols: res.data.map(idol=>{
              //     var date = util.formatTime(new Date(idol.create_time))
              //     idol.date = date
              //     return idol
              //   })
              // })
              page++
              let list = res.data,
                length = list.length,
                idols = that.data.idols,
                idolsID = that.data.idolsID;
              if (isNew && isNew == 1) {
                idols = []
                idolsID = {}
              }
              if (length > 0) {
                for (let i = 0; i < length; i++) {
                  if (!idolsID['idol_' + list[i]._id]) {
                    // 时间转换
                    // list[i].date = that.timeRend(list[i].create_time)
                    idols.push(list[i])
                    idolsID['idol_' + list[i]._id] = 1
                  }
                }
              }
              that.setData({
                idols: idols,
                idolsID: idolsID,
                page: page
              })
            })
        }
      }
    })
  },
  
  //打开详情
  idolTap: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/idol/detail?id=' + id
    })
  },
  idolEdit: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/admin/idol/edit?id=' + id
    })
  },
  //打开
  urlTap: function (e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  _goBack: function () {
    wx.navigateBack({
      delta: 1
    });
  },
  _goAdd: function () {
    wx.navigateTo({
      url: '/pages/my/admin/idol/add',
    });
  }

})